home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
LISTIT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
3KB
|
86 lines
//--- LISTIT.CPP ------------------------- Listing 3 -----------
// Parsing a list from disk; saving it to disk
// See Listing 1 for copyright terms.
//--------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "collect.h"
inline void error (char* msg1 = "", char* msg2 = "",
char* msg3 = "") {
fprintf( stderr, "listit error: %s %s %s\n",
msg1, msg2, msg3 );
exit ( 1 );
}
main(int argc, char ** argv)
{
if( argc < 3 )
error("usage: listit sourcefile destinationfile");
FILE* infile = fopen(argv[1], "r");
if ( !infile )
error("database file", argv[1], "not found");
// Build recordArray by parsing database file:
recordArray* items = new recordArray;
const size = 500;
char line[size];
while ( fgets ( line, size, infile )) { // get lines 'til EOF
const char* quote = "\""; // comma-separated ASCII file
char * name = strtok ( line, quote ); // ANSI C function
// throw away the intervening comma and spaces
strtok ( 0, quote );
char * desc = strtok ( 0, quote ); // get next token in line
items->add ( new record ( name, desc ));
}
fclose ( infile );
// Display and modify the list:
for ( int quitflag = 0; !quitflag; ) {
// display list:
for( int I = 0; I < items->arraySize(); I++ )
printf( "[%d]%s : \"%s\"\n", I,
items->rec(I).Name(), items->rec(I).Description());
printf( "(A)dd item, (D)elete item, (Q)uit " );
switch ( getch() ) {
case 'A': case 'a' :
char name[NameSize], description[DescriptionSize];
printf( "\n name: " ); gets( name );
printf( "\n description: " ); gets( description );
items->add( new record ( name, description ));
break;
case 'D': case 'd' :
printf( "item number to delete? " );
int index = atoi ( gets ( line ));
if ( index < 0 || index >= items->arraySize())
error( "delete index out of range" );
items->destroy ( index );
// create a new one with the copy constructor
// to automatically remove the null spots:
recordArray* temp = items;
items = new recordArray ( *temp );
delete temp;
break;
case 'Q': case 'q' :
quitflag++;
break;
}
}
// Write out the array in simple comma-separated ASCII format:
printf ( "write to destination file %s? (y/n) ", argv[2] );
if ( getch() == 'n' )
error ( "Termination without writing file" );
FILE* outfile = fopen(argv[2], "w");
for ( int i = 0; i < items->arraySize(); i++ )
fprintf ( outfile, "\"%s\",\"%s\"\n",
items->rec(i).Name(), items->rec(i).Description());
fclose ( outfile );
}